#!/bin/sh

##################################################################
##			作者:	杜神				##
##			日期:	2018-01-19			##
##			功能:	DS管理工具集			##
##			version:	20180121			##
##################################################################


APP_PORT1=8080
APP_PORT2=8443
REDIS_PORT1=6379
LOG_NAME=operate-`date +%Y%m%d`.log
LOG_PATH=/app/shlogs/
TOMCAT_HOME=~/environment/tomcat8
MAILS=/app/conf/mail.lst

#应用服务状态检测
app_check()
{
	echo "应用服务状态检测……"|tee -a $LOG_PATH$LOG_NAME
	state='0' #0正常	1异常
	if [ `ps -aux|grep tomcat|grep -v grep|wc -l` -eq '1' ]; then
		echo "tomcat进程正常"|tee -a $LOG_PATH$LOG_NAME
	else
		echo "tomcat进程异常"|tee -a $LOG_PATH$LOG_NAME
		state='1'
	fi
	
	if [ `netstat -an|grep 0.0.0.0:$APP_PORT1|grep LISTEN|wc -l` -eq '1' ]; then
		echo "端口$APP_PORT1状态正常"|tee -a $LOG_PATH$LOG_NAME
	else
		echo "端口$APP_PORT1异常"|tee -a $LOG_PATH$LOG_NAME
		state='1'
	fi

	if [ `netstat -an|grep 0.0.0.0:$APP_PORT2|grep LISTEN|wc -l` -eq '1' ]; then
		echo "端口$APP_PORT2状态正常"|tee -a $LOG_PATH$LOG_NAME
	else
		echo "端口$APP_PORT2异常"|tee -a $LOG_PATH$LOG_NAME
		state='1'
	fi
	
	if [ $state -eq '0' ]; then
		echo "OK!应用服务状态检测正常"|tee -a $LOG_PATH$LOG_NAME
	else
		echo "ERR!应用服务状态存在异常"|tee -a $LOG_PATH$LOG_NAME
	fi
	
	return $state
}

#环境检查
environment_check()
{
	echo "环境检查……"|tee -a $LOG_PATH$LOG_NAME
	state='0' #0正常	1异常
	echo "检查redis状态……"|tee -a $LOG_PATH$LOG_NAME
	echo "1. redis进程"|tee -a $LOG_PATH$LOG_NAME
	if [ `ps -aux|grep redis|grep -v grep|wc -l` -ne '0' ]; then
		echo "redis进程存在`ps -aux|grep redis|grep -v grep|wc -l`个:"|tee -a $LOG_PATH$LOG_NAME
		echo `ps -aux|grep redis|grep -v grep`|tee -a $LOG_PATH$LOG_NAME
	else 
		echo "redis进程异常"|tee -a $LOG_PATH$LOG_NAME
		state='1'
	fi
	echo "2. redis端口"|tee -a $LOG_PATH$LOG_NAME
	if [ `netstat -an| grep $REDIS_PORT1|grep LISTEN|wc -l` -ne '0' ]; then
		echo "redis端口存在`netstat -an| grep $REDIS_PORT1|grep LISTEN|wc -l`个:"|tee -a $LOG_PATH$LOG_NAME
		echo `netstat -an| grep $REDIS_PORT1|grep LISTEN`|tee -a $LOG_PATH$LOG_NAME
		echo "连接数:`netstat -an| grep $REDIS_PORT1|grep ESTABLISHED|wc -l`"|tee -a $LOG_PATH$LOG_NAME
	else 
		echo "redis端口异常"|tee -a $LOG_PATH$LOG_NAME
		state='1'
	fi
	echo "3. redis测试"|tee -a $LOG_PATH$LOG_NAME
	
	if [ `/servers/redis-2.8.19/src/redis-cli  -p $REDIS_PORT1 "ping"` = "PONG" ]; then
		echo "redis测试正常"|tee -a $LOG_PATH$LOG_NAME
	else
		echo "redis测试异常"|tee -a $LOG_PATH$LOG_NAME
		state='1'
	fi
 
	echo "java版本:"|tee -a $LOG_PATH$LOG_NAME
	echo "`java -version`"|tee -a $LOG_PATH$LOG_NAME
	echo "tomcat版本:"|tee -a $LOG_PATH$LOG_NAME
	echo "`$TOMCAT_HOME/bin/version.sh`" | grep -v "Using"|tee -a $LOG_PATH$LOG_NAME
 
	if [ $state -eq '0' ]; then
		echo "OK!环境检查正常"|tee -a $LOG_PATH$LOG_NAME
	else
		echo "ERR!环境检查存在异常"|tee -a $LOG_PATH$LOG_NAME
	fi
	
	return $state
}

#健康检查
health_check()
{
	echo "健康检查……"|tee -a $LOG_PATH$LOG_NAME
	environment_check
	a=$?
	app_check
	b=$?
	if [ $a -eq "0" -a $b -eq "0" ]; then
		echo "OK!健康检查正常"|tee -a $LOG_PATH$LOG_NAME
		return '0'
	fi
	echo "ERR!健康检查存在异常"|tee -a $LOG_PATH$LOG_NAME
	return '1'
}

#启动服务
app_start()
{
	echo "启动服务……"|tee -a $LOG_PATH$LOG_NAME
	echo "检查当前登录用户……"|tee -a $LOG_PATH$LOG_NAME
	if [ `whoami` != "app" ]; then
		echo "ERR!当前登录用户为'`whoami`',请使用应用管理员用户登录!"|tee -a $LOG_PATH$LOG_NAME
		return
	fi
	echo "当前用户:`whoami`"|tee -a $LOG_PATH$LOG_NAME
        if [ `ps -aux|grep tomcat|grep -v grep|wc -l` -ne '0' ]; then
                echo "ERR!tomcat进程已存在,未启动服务!"|tee -a $LOG_PATH$LOG_NAME
	return
        fi 
	environment_check
	a=$?
	if [ $a -eq '1' ]; then
		echo "ERR!环境检查不通过,未启动服务!"|tee -a $LOG_PATH$LOG_NAME
		return
	fi
	echo "打开tomcat……"|tee -a $LOG_PATH$LOG_NAME
	echo `$TOMCAT_HOME/bin/startup.sh`|tee -a $LOG_PATH$LOG_NAME
	echo "OK!服务已启动"|tee -a $LOG_PATH$LOG_NAME
	echo "tomcat 启动完成,是否打开日志(y/n)?"
	read key
	if [ $key = "y" -o $key = "Y" ]; then
		echo "打开日志……"|tee -a $LOG_PATH$LOG_NAME
		tail -f $TOMCAT_HOME/logs/catalina.out
	fi

}

#停止服务
app_stop()
{
	echo "停止服务……"
	if [ `ps -aux|grep tomcat|grep -v grep|wc -l` -eq '0' ]; then
		echo "tomcat进程不存在,无需停止!"|tee -a $LOG_PATH$LOG_NAME
		return
	fi
	echo `$TOMCAT_HOME/bin/shutdown.sh`|tee -a $LOG_PATH$LOG_NAME
	sleep 8
	if [ `ps -aux|grep tomcat|grep -v grep|wc -l` -eq '0' ]; then
		echo "成功停止服务!"|tee -a $LOG_PATH$LOG_NAME
		return
	fi
	for pid in `ps -aux|grep tomcat|grep -v grep|awk '{print $2}'`;do
		kill -9 $pid
		echo "已杀死进程$pid"|tee -a $LOG_PATH$LOG_NAME
	done
	if [ `ps -aux|grep tomcat|grep -v grep|wc -l` -eq '0' ]; then
		echo "OK!成功停止服务!"|tee -a $LOG_PATH$LOG_NAME
		return
	fi
	echo "ERR!停止服务异常,需手动停止!"|tee -a $LOG_PATH$LOG_NAME
}

#主菜单
menu()  
{       
	while [ "1" ]
	do      
		clear
		echo "功能菜单:"
		echo "1. 健康检查"
		echo "2. 应用服务状态检测"
		echo "3. 环境检测"
		echo "4. 启动服务"
		echo "5. 停止服务"
		echo "0. 退出"
		echo "select==>>"
		tput cup 7 10
		tput cnorm 
		read choice
		case $choice in
		0) clear
			break;;
		1) echo "开始健康检查(y/n)?"
			read key
			if [ $key = "y" -o $key = "Y" ]; then
				echo "+++++++++++++++++++++++++++健康检查-time:`date '+%Y-%m-%d %H:%M:%S'`+++++++++++++++++++++++++++"|tee -a $LOG_PATH$LOG_NAME
				health_check
			fi
			echo "按任意键继续……"
			read key
			continue;;
		2) echo "开始应用服务状态检测(y/n)?"
			read key
			if [ $key = "y" -o $key = "Y" ]; then
				echo "+++++++++++++++++++++++应用服务状态检测-time:`date '+%Y-%m-%d %H:%M:%S'`+++++++++++++++++++++++"|tee -a $LOG_PATH$LOG_NAME
				app_check
			fi
			echo "按任意键继续……"
			read key
			continue;;
		3) echo "开始环境检测(y/n)?"
			read key
			if [ $key = "y" -o $key = "Y" ]; then
				echo "+++++++++++++++++++++++++++环境检测-time:`date '+%Y-%m-%d %H:%M:%S'`+++++++++++++++++++++++++++"|tee -a $LOG_PATH$LOG_NAME
				environment_check
			fi
			echo "按任意键继续……"
			read key
			continue;;
		4) echo "启动服务(y/n)?"
			read key
			if [ $key = "y" -o $key = "Y" ]; then
				echo "+++++++++++++++++++++++++++启动服务-time:`date '+%Y-%m-%d %H:%M:%S'`+++++++++++++++++++++++++++"|tee -a $LOG_PATH$LOG_NAME
				app_start
			fi
			echo "按任意键继续……"
			read key
			continue;;
		5) echo "停止服务(y/n)?"
			read key
			if [ $key = "y" -o $key = "Y" ]; then
				echo "+++++++++++++++++++++++++++停止服务-time:`date '+%Y-%m-%d %H:%M:%S'`+++++++++++++++++++++++++++"|tee -a $LOG_PATH$LOG_NAME
				app_stop
			fi
			echo "按任意键继续……"
			read key
			continue;;
		esac    
	done    
}       

while getopts 'dm' opt;
do
	case $opt in
		d) 
			echo "日常周期巡检------>>>>-time:`date '+%Y-%m-%d %H:%M:%S'`"|tee -a $LOG_PATH$LOG_NAME
			health_check >/app/tmp/daily.log
			for mail in `cat $MAILS` ;do
				mail -s "DS服务器周期巡检报告 `date '+%Y-%m-%d %H:%M:%S'`" "$mail" < /app/tmp/daily.log
				echo "已发送通知邮件:$mail"|tee -a $LOG_PATH$LOG_NAME
			done
			echo "OK!日常周期巡检完成------>>>>-time:`date '+%Y-%m-%d %H:%M:%S'`"|tee -a $LOG_PATH$LOG_NAME;;
		m)
			menu;;
		?)
			echo "Usage:`basename $0` [options] filename"
	esac
done

